package com.example.apidocserver.infrastructure.util.word;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;
import java.util.List;

/**
 * 表格设置工具
 *
 * @author xyqlg
 */
public class WordTableUtil {
    /**
     * 字段宽度
     */
    public final static String[] ROW_WIDTH_PERCENTAGES = {"21%", "12%", "12%", "55%"};

    /**
     * 设置表格整体宽度
     *
     * @param table            表格对象
     * @param row              第几行
     * @param widthPercentages 列宽数组
     */
    public static void setTableColumnWidth(XWPFTable table, int row, String[] widthPercentages) {
        // 设置表格列宽
        CTTblWidth tableWidth = table.getCTTbl().addNewTblPr().addNewTblW();
        tableWidth.setType(STTblWidth.PCT);
        tableWidth.setW(BigInteger.valueOf(5000));

        // 获取当前行
        XWPFTableRow tableRow = table.getRow(row);
        // 设置行的列宽比例
        for (int i = 0; i < widthPercentages.length; i++) {
            tableRow.getCell(i).setWidth(widthPercentages[i]);
        }
    }

    /**
     * 设置表格整体宽度
     *
     * @param table 表格对象
     * @param row   第几行
     */
    public static void setTableColumnWidth(XWPFTable table, int row) {
        setTableColumnWidth(table, row, ROW_WIDTH_PERCENTAGES);
    }

    public static XWPFTableCell setCellText(XWPFTableRow row, int pos, String field, boolean boldFlag) {
        XWPFTableCell cell1 = row.getCell(pos);
        cell1.setText(field);
        if (boldFlag) {
            setCellTypeBold(cell1);
        }
        return cell1;
    }

    public static void setCellTypeBold(XWPFTableCell cell) {
        List<XWPFParagraph> paragraphs = cell.getParagraphs();
        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        for (XWPFParagraph paragraph : paragraphs) {
            for (XWPFRun run : paragraph.getRuns()) {
                run.setBold(true);
            }
        }
    }

    public static void setTitleStyle(XWPFTable table, int row) {
        XWPFTableRow row1 = table.getRow(row);
        List<XWPFTableCell> tableCells = row1.getTableCells();

        for (XWPFTableCell tableCell : tableCells) {
            tableCell.setColor("FF0000");
            List<XWPFParagraph> paragraphs = tableCell.getParagraphs();
            for (XWPFParagraph paragraph : paragraphs) {
                XWPFRun run = paragraph.createRun();
                run.setText(tableCell.getText());
                run.setFontSize(20);
                run.setBold(true);
                tableCell.setText(null);
            }
        }
    }

    /**
     * 设置表格表头
     *
     * @param table    表格信息
     * @param rowIndex 行下标
     * @param titles   标题字段数组
     */
    public static void setTitle(XWPFTable table, int rowIndex, String[] titles) {
        for (int i = 0; i < titles.length; i++) {
            XWPFTableRow row = table.getRow(rowIndex);
            row.setHeight(400);
            XWPFTableCell cell = row.getCell(i);
            cell.setColor("d2d5d2");
            cell.setText(titles[i]);
            List<XWPFParagraph> paragraphs = cell.getParagraphs();
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            for (XWPFParagraph paragraph : paragraphs) {
                for (XWPFRun run : paragraph.getRuns()) {
                    run.setFontSize(WordConstants.TABLE_SIZE);
                    run.setBold(true);
                }
            }
        }
    }
}
